package com.chuangyang.fixboxmaster.ui.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuangyang.fixboxmaster.R;

/**
 * Created by Administrator on 2015/6/10.
 */
public class ApplianceListItem extends LinearLayout {

    private TextView listItemDescription;

    private void init(){
        setClickable(true);
        setFocusable(true);
    }


    private void drawView(Context context,AttributeSet attrs){
        TypedArray a = null;
        View view ;
        try {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.appliance_list_item, this, true);
            ImageView listItemLeftIcon = (ImageView) view.findViewById(R.id.list_item_left_icon);
            ImageView listItemRightIcon = (ImageView) view.findViewById(R.id.list_item_right_icon);
            listItemDescription = (TextView) view.findViewById(R.id.list_item_des);
            a = context.obtainStyledAttributes(attrs, R.styleable.ApplianceListItemView);
            Drawable srcLeft = a.getDrawable(R.styleable.ApplianceListItemView_left_src);
            Drawable srcRight = a.getDrawable(R.styleable.ApplianceListItemView_right_src);
            String description=a.getString(R.styleable.ApplianceListItemView_desc);
            if(srcLeft!=null){
                listItemLeftIcon.setVisibility(VISIBLE);
                listItemLeftIcon.setImageDrawable(srcLeft);
            }
            if(srcRight!=null){
                listItemRightIcon.setVisibility(VISIBLE);
                listItemRightIcon.setImageDrawable(srcRight);
            }
            if(description!=null){
                listItemDescription.setText(description);
            }

        } catch (Exception e) {
        } finally {
            if (a != null)
                a.recycle();
        }
    }

    public void setDescription(String desc){

        if(desc!=null){
            listItemDescription.setText(desc);
        }

    }

    public String getDescription(){

        if(listItemDescription!=null){

            return listItemDescription.getText().toString();
        }
        return "";

    }


    public ApplianceListItem(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        drawView(context,attrs);
    }
}
